{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: tensorflow in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (2.11.0)\n",
      "Requirement already satisfied: tensorflow-intel==2.11.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow) (2.11.0)\n",
      "Requirement already satisfied: libclang>=13.0.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (14.0.6)\n",
      "Requirement already satisfied: gast<=0.4.0,>=0.2.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (0.4.0)\n",
      "Requirement already satisfied: typing-extensions>=3.6.6 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (4.4.0)\n",
      "Requirement already satisfied: grpcio<2.0,>=1.24.3 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.51.1)\n",
      "Requirement already satisfied: protobuf<3.20,>=3.9.2 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (3.19.6)\n",
      "Requirement already satisfied: opt-einsum>=2.3.2 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (3.3.0)\n",
      "Requirement already satisfied: setuptools in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (60.2.0)\n",
      "Requirement already satisfied: numpy>=1.20 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.23.5)\n",
      "Requirement already satisfied: tensorflow-io-gcs-filesystem>=0.23.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (0.28.0)\n",
      "Requirement already satisfied: six>=1.12.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.16.0)\n",
      "Requirement already satisfied: absl-py>=1.0.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.3.0)\n",
      "Requirement already satisfied: google-pasta>=0.1.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (0.2.0)\n",
      "Requirement already satisfied: keras<2.12,>=2.11.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (2.11.0)\n",
      "Requirement already satisfied: packaging in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (22.0)\n",
      "Requirement already satisfied: termcolor>=1.1.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (2.1.1)\n",
      "Requirement already satisfied: tensorflow-estimator<2.12,>=2.11.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (2.11.0)\n",
      "Requirement already satisfied: wrapt>=1.11.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.14.1)\n",
      "Requirement already satisfied: flatbuffers>=2.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (22.12.6)\n",
      "Requirement already satisfied: astunparse>=1.6.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (1.6.3)\n",
      "Requirement already satisfied: tensorboard<2.12,>=2.11 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (2.11.0)\n",
      "Requirement already satisfied: h5py>=2.9.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorflow-intel==2.11.0->tensorflow) (3.7.0)\n",
      "Requirement already satisfied: wheel<1.0,>=0.23.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from astunparse>=1.6.0->tensorflow-intel==2.11.0->tensorflow) (0.37.1)\n",
      "Requirement already satisfied: tensorboard-plugin-wit>=1.6.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (1.8.1)\n",
      "Requirement already satisfied: google-auth<3,>=1.6.3 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.15.0)\n",
      "Requirement already satisfied: tensorboard-data-server<0.7.0,>=0.6.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (0.6.1)\n",
      "Requirement already satisfied: google-auth-oauthlib<0.5,>=0.4.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (0.4.6)\n",
      "Requirement already satisfied: requests<3,>=2.21.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.28.1)\n",
      "Requirement already satisfied: markdown>=2.6.8 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (3.4.1)\n",
      "Requirement already satisfied: werkzeug>=1.0.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.2.2)\n",
      "Requirement already satisfied: pyasn1-modules>=0.2.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (0.2.8)\n",
      "Requirement already satisfied: cachetools<6.0,>=2.0.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (5.2.0)\n",
      "Requirement already satisfied: rsa<5,>=3.1.4 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (4.9)\n",
      "Requirement already satisfied: requests-oauthlib>=0.7.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (1.3.1)\n",
      "Requirement already satisfied: importlib-metadata>=4.4 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from markdown>=2.6.8->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (5.1.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2022.12.7)\n",
      "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (3.4)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (1.26.13)\n",
      "Requirement already satisfied: charset-normalizer<3,>=2 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from requests<3,>=2.21.0->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.1.1)\n",
      "Requirement already satisfied: MarkupSafe>=2.1.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from werkzeug>=1.0.1->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (2.1.1)\n",
      "Requirement already satisfied: zipp>=0.5 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from importlib-metadata>=4.4->markdown>=2.6.8->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (3.11.0)\n",
      "Requirement already satisfied: pyasn1<0.5.0,>=0.4.6 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from pyasn1-modules>=0.2.1->google-auth<3,>=1.6.3->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (0.4.8)\n",
      "Requirement already satisfied: oauthlib>=3.0.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.12,>=2.11->tensorflow-intel==2.11.0->tensorflow) (3.2.2)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: You are using pip version 21.3.1; however, version 22.3.1 is available.\n",
      "You should consider upgrading via the 'C:\\Users\\Fer_U\\PycharmProjects\\CyberattacksAttention\\venv\\Scripts\\python.exe -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "!pip install tensorflow"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pandas in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (1.5.2)\n",
      "Requirement already satisfied: pytz>=2020.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from pandas) (2022.6)\n",
      "Requirement already satisfied: python-dateutil>=2.8.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from pandas) (2.8.2)\n",
      "Requirement already satisfied: numpy>=1.20.3 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from pandas) (1.23.5)\n",
      "Requirement already satisfied: six>=1.5 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: You are using pip version 21.3.1; however, version 22.3.1 is available.\n",
      "You should consider upgrading via the 'C:\\Users\\Fer_U\\PycharmProjects\\CyberattacksAttention\\venv\\Scripts\\python.exe -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "!pip install pandas"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: neural_structured_learning in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (1.4.0)\n",
      "Requirement already satisfied: attrs in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from neural_structured_learning) (22.1.0)\n",
      "Requirement already satisfied: absl-py in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from neural_structured_learning) (1.3.0)\n",
      "Requirement already satisfied: scipy in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from neural_structured_learning) (1.9.3)\n",
      "Requirement already satisfied: six in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from neural_structured_learning) (1.16.0)\n",
      "Requirement already satisfied: numpy<1.26.0,>=1.18.5 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from scipy->neural_structured_learning) (1.23.5)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: You are using pip version 21.3.1; however, version 22.3.1 is available.\n",
      "You should consider upgrading via the 'C:\\Users\\Fer_U\\PycharmProjects\\CyberattacksAttention\\venv\\Scripts\\python.exe -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "!pip install neural_structured_learning"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: scikit_learn in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (1.2.0)\n",
      "Requirement already satisfied: joblib>=1.1.1 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from scikit_learn) (1.2.0)\n",
      "Requirement already satisfied: numpy>=1.17.3 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from scikit_learn) (1.23.5)\n",
      "Requirement already satisfied: threadpoolctl>=2.0.0 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from scikit_learn) (3.1.0)\n",
      "Requirement already satisfied: scipy>=1.3.2 in c:\\users\\fer_u\\pycharmprojects\\cyberattacksattention\\venv\\lib\\site-packages (from scikit_learn) (1.9.3)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: You are using pip version 21.3.1; however, version 22.3.1 is available.\n",
      "You should consider upgrading via the 'C:\\Users\\Fer_U\\PycharmProjects\\CyberattacksAttention\\venv\\Scripts\\python.exe -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "!pip install scikit_learn"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "executionInfo": {
     "elapsed": 4460,
     "status": "ok",
     "timestamp": 1670604423667,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "bTL3Ufo0t487",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import pandas as pd\n",
    "import neural_structured_learning as nsl\n",
    "import numpy as np\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn import preprocessing\n",
    "from sklearn.model_selection import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "executionInfo": {
     "elapsed": 1214,
     "status": "ok",
     "timestamp": 1670604426852,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "xdB9GixktNz0",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "df = pd.read_csv('CSE_CIC_IDS_2018/03-02-2018.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 300
    },
    "executionInfo": {
     "elapsed": 14,
     "status": "ok",
     "timestamp": 1670604426856,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "856UWFEmyUms",
    "outputId": "a5a2bcaf-4a37-454e-eb77-2ffe6b9b956f",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "   Dst Port  Protocol            Timestamp  Flow Duration  Tot Fwd Pkts  \\\n0       443         6  02/03/2018 08:47:38         141385             9   \n1     49684         6  02/03/2018 08:47:38            281             2   \n2       443         6  02/03/2018 08:47:40         279824            11   \n3       443         6  02/03/2018 08:47:40            132             2   \n4       443         6  02/03/2018 08:47:41         274016             9   \n\n   Tot Bwd Pkts  TotLen Fwd Pkts  TotLen Bwd Pkts  Fwd Pkt Len Max  \\\n0             7              553           3773.0              202   \n1             1               38              0.0               38   \n2            15             1086          10527.0              385   \n3             0                0              0.0                0   \n4            13             1285           6141.0              517   \n\n   Fwd Pkt Len Min  ...  Fwd Seg Size Min  Active Mean  Active Std  \\\n0                0  ...                20          0.0         0.0   \n1                0  ...                20          0.0         0.0   \n2                0  ...                20          0.0         0.0   \n3                0  ...                20          0.0         0.0   \n4                0  ...                20          0.0         0.0   \n\n   Active Max  Active Min  Idle Mean  Idle Std  Idle Max  Idle Min   Label  \n0         0.0         0.0        0.0       0.0       0.0       0.0  Benign  \n1         0.0         0.0        0.0       0.0       0.0       0.0  Benign  \n2         0.0         0.0        0.0       0.0       0.0       0.0  Benign  \n3         0.0         0.0        0.0       0.0       0.0       0.0  Benign  \n4         0.0         0.0        0.0       0.0       0.0       0.0  Benign  \n\n[5 rows x 80 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Dst Port</th>\n      <th>Protocol</th>\n      <th>Timestamp</th>\n      <th>Flow Duration</th>\n      <th>Tot Fwd Pkts</th>\n      <th>Tot Bwd Pkts</th>\n      <th>TotLen Fwd Pkts</th>\n      <th>TotLen Bwd Pkts</th>\n      <th>Fwd Pkt Len Max</th>\n      <th>Fwd Pkt Len Min</th>\n      <th>...</th>\n      <th>Fwd Seg Size Min</th>\n      <th>Active Mean</th>\n      <th>Active Std</th>\n      <th>Active Max</th>\n      <th>Active Min</th>\n      <th>Idle Mean</th>\n      <th>Idle Std</th>\n      <th>Idle Max</th>\n      <th>Idle Min</th>\n      <th>Label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>443</td>\n      <td>6</td>\n      <td>02/03/2018 08:47:38</td>\n      <td>141385</td>\n      <td>9</td>\n      <td>7</td>\n      <td>553</td>\n      <td>3773.0</td>\n      <td>202</td>\n      <td>0</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>Benign</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>49684</td>\n      <td>6</td>\n      <td>02/03/2018 08:47:38</td>\n      <td>281</td>\n      <td>2</td>\n      <td>1</td>\n      <td>38</td>\n      <td>0.0</td>\n      <td>38</td>\n      <td>0</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>Benign</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>443</td>\n      <td>6</td>\n      <td>02/03/2018 08:47:40</td>\n      <td>279824</td>\n      <td>11</td>\n      <td>15</td>\n      <td>1086</td>\n      <td>10527.0</td>\n      <td>385</td>\n      <td>0</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>Benign</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>443</td>\n      <td>6</td>\n      <td>02/03/2018 08:47:40</td>\n      <td>132</td>\n      <td>2</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0.0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>Benign</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>443</td>\n      <td>6</td>\n      <td>02/03/2018 08:47:41</td>\n      <td>274016</td>\n      <td>9</td>\n      <td>13</td>\n      <td>1285</td>\n      <td>6141.0</td>\n      <td>517</td>\n      <td>0</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>Benign</td>\n    </tr>\n  </tbody>\n</table>\n<p>5 rows × 80 columns</p>\n</div>"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "executionInfo": {
     "elapsed": 9,
     "status": "ok",
     "timestamp": 1670604426856,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "mkez4dRDyZ4L",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "label_encoder = LabelEncoder()\n",
    "df = df.replace([np.inf, -np.inf], np.nan)\n",
    "df = df.dropna()\n",
    "df = df.reset_index(drop=True)\n",
    "df = df.drop(['Timestamp'], axis=1)\n",
    "\n",
    "features = len(df.columns) - 1\n",
    "\n",
    "df['Label'] = label_encoder.fit_transform(df['Label'])\n",
    "classes = df['Label'].nunique()\n",
    "df = df.astype(float)\n",
    "df = df.astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 488
    },
    "executionInfo": {
     "elapsed": 10,
     "status": "ok",
     "timestamp": 1670604426857,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "6wz-53mHnm7p",
    "outputId": "fe64ae3c-8710-4fbe-fc93-ff39924faffd",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "         Dst Port  Protocol  Flow Duration  Tot Fwd Pkts  Tot Bwd Pkts  \\\n0             443         6         141385             9             7   \n1           49684         6            281             2             1   \n2             443         6         279824            11            15   \n3             443         6            132             2             0   \n4             443         6         274016             9            13   \n...           ...       ...            ...           ...           ...   \n1044520      3389         6        3982183            14             8   \n1044521      3389         6        3802316            14             8   \n1044522      3389         6        4004239            14             8   \n1044523      3389         6        3998435            14             8   \n1044524      3389         6        3972651            14             8   \n\n         TotLen Fwd Pkts  TotLen Bwd Pkts  Fwd Pkt Len Max  Fwd Pkt Len Min  \\\n0                    553             3773              202                0   \n1                     38                0               38                0   \n2                   1086            10527              385                0   \n3                      0                0                0                0   \n4                   1285             6141              517                0   \n...                  ...              ...              ...              ...   \n1044520             1442             1731              725                0   \n1044521             1440             1731              725                0   \n1044522             1459             1731              741                0   \n1044523             1459             1731              741                0   \n1044524             1439             1731              725                0   \n\n         Fwd Pkt Len Mean  ...  Fwd Seg Size Min  Active Mean  Active Std  \\\n0                      61  ...                20            0           0   \n1                      19  ...                20            0           0   \n2                      98  ...                20            0           0   \n3                       0  ...                20            0           0   \n4                     142  ...                20            0           0   \n...                   ...  ...               ...          ...         ...   \n1044520               103  ...                20            0           0   \n1044521               102  ...                20            0           0   \n1044522               104  ...                20            0           0   \n1044523               104  ...                20            0           0   \n1044524               102  ...                20            0           0   \n\n         Active Max  Active Min  Idle Mean  Idle Std  Idle Max  Idle Min  \\\n0                 0           0          0         0         0         0   \n1                 0           0          0         0         0         0   \n2                 0           0          0         0         0         0   \n3                 0           0          0         0         0         0   \n4                 0           0          0         0         0         0   \n...             ...         ...        ...       ...       ...       ...   \n1044520           0           0          0         0         0         0   \n1044521           0           0          0         0         0         0   \n1044522           0           0          0         0         0         0   \n1044523           0           0          0         0         0         0   \n1044524           0           0          0         0         0         0   \n\n         Label  \n0            0  \n1            0  \n2            0  \n3            0  \n4            0  \n...        ...  \n1044520      0  \n1044521      0  \n1044522      0  \n1044523      0  \n1044524      0  \n\n[1044525 rows x 79 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Dst Port</th>\n      <th>Protocol</th>\n      <th>Flow Duration</th>\n      <th>Tot Fwd Pkts</th>\n      <th>Tot Bwd Pkts</th>\n      <th>TotLen Fwd Pkts</th>\n      <th>TotLen Bwd Pkts</th>\n      <th>Fwd Pkt Len Max</th>\n      <th>Fwd Pkt Len Min</th>\n      <th>Fwd Pkt Len Mean</th>\n      <th>...</th>\n      <th>Fwd Seg Size Min</th>\n      <th>Active Mean</th>\n      <th>Active Std</th>\n      <th>Active Max</th>\n      <th>Active Min</th>\n      <th>Idle Mean</th>\n      <th>Idle Std</th>\n      <th>Idle Max</th>\n      <th>Idle Min</th>\n      <th>Label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>443</td>\n      <td>6</td>\n      <td>141385</td>\n      <td>9</td>\n      <td>7</td>\n      <td>553</td>\n      <td>3773</td>\n      <td>202</td>\n      <td>0</td>\n      <td>61</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>49684</td>\n      <td>6</td>\n      <td>281</td>\n      <td>2</td>\n      <td>1</td>\n      <td>38</td>\n      <td>0</td>\n      <td>38</td>\n      <td>0</td>\n      <td>19</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>443</td>\n      <td>6</td>\n      <td>279824</td>\n      <td>11</td>\n      <td>15</td>\n      <td>1086</td>\n      <td>10527</td>\n      <td>385</td>\n      <td>0</td>\n      <td>98</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>443</td>\n      <td>6</td>\n      <td>132</td>\n      <td>2</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>443</td>\n      <td>6</td>\n      <td>274016</td>\n      <td>9</td>\n      <td>13</td>\n      <td>1285</td>\n      <td>6141</td>\n      <td>517</td>\n      <td>0</td>\n      <td>142</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>1044520</th>\n      <td>3389</td>\n      <td>6</td>\n      <td>3982183</td>\n      <td>14</td>\n      <td>8</td>\n      <td>1442</td>\n      <td>1731</td>\n      <td>725</td>\n      <td>0</td>\n      <td>103</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1044521</th>\n      <td>3389</td>\n      <td>6</td>\n      <td>3802316</td>\n      <td>14</td>\n      <td>8</td>\n      <td>1440</td>\n      <td>1731</td>\n      <td>725</td>\n      <td>0</td>\n      <td>102</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1044522</th>\n      <td>3389</td>\n      <td>6</td>\n      <td>4004239</td>\n      <td>14</td>\n      <td>8</td>\n      <td>1459</td>\n      <td>1731</td>\n      <td>741</td>\n      <td>0</td>\n      <td>104</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1044523</th>\n      <td>3389</td>\n      <td>6</td>\n      <td>3998435</td>\n      <td>14</td>\n      <td>8</td>\n      <td>1459</td>\n      <td>1731</td>\n      <td>741</td>\n      <td>0</td>\n      <td>104</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1044524</th>\n      <td>3389</td>\n      <td>6</td>\n      <td>3972651</td>\n      <td>14</td>\n      <td>8</td>\n      <td>1439</td>\n      <td>1731</td>\n      <td>725</td>\n      <td>0</td>\n      <td>102</td>\n      <td>...</td>\n      <td>20</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n<p>1044525 rows × 79 columns</p>\n</div>"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "executionInfo": {
     "elapsed": 4,
     "status": "ok",
     "timestamp": 1670604427264,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "9s_HaYjkzuKk",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "y = df.pop('Label')\n",
    "X = df\n",
    "\n",
    "normalizer_scaler = preprocessing.Normalizer()\n",
    "x_scaled = normalizer_scaler.fit_transform(X)\n",
    "X = pd.DataFrame(x_scaled)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "executionInfo": {
     "elapsed": 4,
     "status": "ok",
     "timestamp": 1670604427264,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "l-9LdOome2ck",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "executionInfo": {
     "elapsed": 3,
     "status": "ok",
     "timestamp": 1670604427264,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "FC6lXk4Az3yB",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "X_train = tf.convert_to_tensor(X_train)\n",
    "y_train = tf.convert_to_tensor(y_train)\n",
    "\n",
    "X_test = tf.convert_to_tensor(X_test)\n",
    "y_test = tf.convert_to_tensor(y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "executionInfo": {
     "elapsed": 333,
     "status": "ok",
     "timestamp": 1670604427594,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "SP8ckOayytne",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "input = tf.keras.layers.Input((features,), name='feature')\n",
    "\n",
    "n1 = tf.keras.layers.Dense(16)(input)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(32)(n1)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(64)(n1)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(128)(n1)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(256)(n1)\n",
    "\n",
    "n1 = tf.keras.layers.MultiHeadAttention(num_heads=int(256/8), key_dim=256, value_dim=256, attention_axes=1)(n1, n1, n1)\n",
    "\n",
    "n1 = tf.keras.layers.Dense(256)(n1)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(128)(n1)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(64)(n1)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(32)(n1)\n",
    "n1 = tf.keras.layers.Dropout(0.25)(n1)\n",
    "n1 = tf.keras.layers.Dense(16)(n1)\n",
    "\n",
    "output = tf.keras.layers.Dense(classes, activation='softmax')(n1)\n",
    "\n",
    "model = tf.keras.Model(inputs=input, outputs=output)\n",
    "\n",
    "adv_config = nsl.configs.make_adv_reg_config(multiplier=0.2, adv_step_size=0.05)\n",
    "adv_model = nsl.keras.AdversarialRegularization(model, adv_config=adv_config)\n",
    "\n",
    "# Compile, train, and evaluate.\n",
    "adv_model.compile(optimizer='adam',\n",
    "                  loss='sparse_categorical_crossentropy',\n",
    "                  metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 269,
     "status": "ok",
     "timestamp": 1670604427855,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "-EUxz4vRy7YP",
    "outputId": "1c8c1a81-6e6b-4129-f8a2-9d22f904cb87",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"model\"\n",
      "__________________________________________________________________________________________________\n",
      " Layer (type)                   Output Shape         Param #     Connected to                     \n",
      "==================================================================================================\n",
      " feature (InputLayer)           [(None, 78)]         0           []                               \n",
      "                                                                                                  \n",
      " dense (Dense)                  (None, 16)           1264        ['feature[0][0]']                \n",
      "                                                                                                  \n",
      " dropout (Dropout)              (None, 16)           0           ['dense[0][0]']                  \n",
      "                                                                                                  \n",
      " dense_1 (Dense)                (None, 32)           544         ['dropout[0][0]']                \n",
      "                                                                                                  \n",
      " dropout_1 (Dropout)            (None, 32)           0           ['dense_1[0][0]']                \n",
      "                                                                                                  \n",
      " dense_2 (Dense)                (None, 64)           2112        ['dropout_1[0][0]']              \n",
      "                                                                                                  \n",
      " dropout_2 (Dropout)            (None, 64)           0           ['dense_2[0][0]']                \n",
      "                                                                                                  \n",
      " dense_3 (Dense)                (None, 128)          8320        ['dropout_2[0][0]']              \n",
      "                                                                                                  \n",
      " dropout_3 (Dropout)            (None, 128)          0           ['dense_3[0][0]']                \n",
      "                                                                                                  \n",
      " dense_4 (Dense)                (None, 256)          33024       ['dropout_3[0][0]']              \n",
      "                                                                                                  \n",
      " multi_head_attention (MultiHea  (None, 256)         8413440     ['dense_4[0][0]',                \n",
      " dAttention)                                                      'dense_4[0][0]',                \n",
      "                                                                  'dense_4[0][0]']                \n",
      "                                                                                                  \n",
      " dense_5 (Dense)                (None, 256)          65792       ['multi_head_attention[0][0]']   \n",
      "                                                                                                  \n",
      " dropout_4 (Dropout)            (None, 256)          0           ['dense_5[0][0]']                \n",
      "                                                                                                  \n",
      " dense_6 (Dense)                (None, 128)          32896       ['dropout_4[0][0]']              \n",
      "                                                                                                  \n",
      " dropout_5 (Dropout)            (None, 128)          0           ['dense_6[0][0]']                \n",
      "                                                                                                  \n",
      " dense_7 (Dense)                (None, 64)           8256        ['dropout_5[0][0]']              \n",
      "                                                                                                  \n",
      " dropout_6 (Dropout)            (None, 64)           0           ['dense_7[0][0]']                \n",
      "                                                                                                  \n",
      " dense_8 (Dense)                (None, 32)           2080        ['dropout_6[0][0]']              \n",
      "                                                                                                  \n",
      " dropout_7 (Dropout)            (None, 32)           0           ['dense_8[0][0]']                \n",
      "                                                                                                  \n",
      " dense_9 (Dense)                (None, 16)           528         ['dropout_7[0][0]']              \n",
      "                                                                                                  \n",
      " dense_10 (Dense)               (None, 2)            34          ['dense_9[0][0]']                \n",
      "                                                                                                  \n",
      "==================================================================================================\n",
      "Total params: 8,568,290\n",
      "Trainable params: 8,568,290\n",
      "Non-trainable params: 0\n",
      "__________________________________________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 48
    },
    "executionInfo": {
     "elapsed": 447,
     "status": "ok",
     "timestamp": 1670604428298,
     "user": {
      "displayName": "Fernando José Rendón Segador",
      "userId": "09288482551460164544"
     },
     "user_tz": -60
    },
    "id": "844W7YPn82Gw",
    "outputId": "ea88bcec-2ee8-4229-e43f-6c1a767fdc89",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You must install pydot (`pip install pydot`) and install graphviz (see instructions at https://graphviz.gitlab.io/download/) for plot_model to work.\n"
     ]
    }
   ],
   "source": [
    "tf.keras.utils.plot_model(model, \n",
    "                          show_shapes=True,\n",
    "                          show_dtype=True,\n",
    "                          show_layer_names=True,\n",
    "                          rankdir='LR')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "RjM9Xf2Ry-lq",
    "outputId": "3f4f80c6-a5ac-4e5e-8981-be5081cd7e3c",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/10\n",
      "WARNING:tensorflow:From C:\\Users\\Fer_U\\PycharmProjects\\CyberattacksAttention\\venv\\lib\\site-packages\\tensorflow\\python\\autograph\\pyct\\static_analysis\\liveness.py:83: Analyzer.lamba_check (from tensorflow.python.autograph.pyct.static_analysis.liveness) is deprecated and will be removed after 2023-09-23.\n",
      "Instructions for updating:\n",
      "Lambda fuctions will be no more assumed to be used in the statement where they are used, or at least in the same block. https://github.com/tensorflow/tensorflow/issues/56089\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:absl:Cannot perturb non-Tensor input: dict_keys(['label'])\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1633/1633 [==============================] - 592s 360ms/step - loss: 0.0794 - sparse_categorical_crossentropy: 0.0492 - sparse_categorical_accuracy: 0.9864 - scaled_adversarial_loss: 0.0302 - val_loss: 0.0406 - val_sparse_categorical_crossentropy: 0.0141 - val_sparse_categorical_accuracy: 0.9954 - val_scaled_adversarial_loss: 0.0264\n",
      "Epoch 2/10\n",
      "1633/1633 [==============================] - 580s 355ms/step - loss: 0.0421 - sparse_categorical_crossentropy: 0.0170 - sparse_categorical_accuracy: 0.9957 - scaled_adversarial_loss: 0.0251 - val_loss: 0.0395 - val_sparse_categorical_crossentropy: 0.0135 - val_sparse_categorical_accuracy: 0.9958 - val_scaled_adversarial_loss: 0.0260\n",
      "Epoch 3/10\n",
      "1633/1633 [==============================] - 578s 354ms/step - loss: 0.0316 - sparse_categorical_crossentropy: 0.0132 - sparse_categorical_accuracy: 0.9971 - scaled_adversarial_loss: 0.0183 - val_loss: 0.0171 - val_sparse_categorical_crossentropy: 0.0059 - val_sparse_categorical_accuracy: 0.9984 - val_scaled_adversarial_loss: 0.0112\n",
      "Epoch 4/10\n",
      "1633/1633 [==============================] - 572s 350ms/step - loss: 0.0202 - sparse_categorical_crossentropy: 0.0089 - sparse_categorical_accuracy: 0.9983 - scaled_adversarial_loss: 0.0114 - val_loss: 0.0176 - val_sparse_categorical_crossentropy: 0.0060 - val_sparse_categorical_accuracy: 0.9987 - val_scaled_adversarial_loss: 0.0116\n",
      "Epoch 5/10\n",
      "1633/1633 [==============================] - 579s 354ms/step - loss: 0.0183 - sparse_categorical_crossentropy: 0.0081 - sparse_categorical_accuracy: 0.9984 - scaled_adversarial_loss: 0.0102 - val_loss: 0.0133 - val_sparse_categorical_crossentropy: 0.0052 - val_sparse_categorical_accuracy: 0.9988 - val_scaled_adversarial_loss: 0.0081\n",
      "Epoch 6/10\n",
      "1633/1633 [==============================] - 567s 347ms/step - loss: 0.0153 - sparse_categorical_crossentropy: 0.0073 - sparse_categorical_accuracy: 0.9987 - scaled_adversarial_loss: 0.0079 - val_loss: 0.0120 - val_sparse_categorical_crossentropy: 0.0051 - val_sparse_categorical_accuracy: 0.9989 - val_scaled_adversarial_loss: 0.0068\n",
      "Epoch 7/10\n",
      "1633/1633 [==============================] - 579s 355ms/step - loss: 0.0140 - sparse_categorical_crossentropy: 0.0067 - sparse_categorical_accuracy: 0.9989 - scaled_adversarial_loss: 0.0073 - val_loss: 0.0134 - val_sparse_categorical_crossentropy: 0.0045 - val_sparse_categorical_accuracy: 0.9990 - val_scaled_adversarial_loss: 0.0089\n",
      "Epoch 8/10\n",
      "1633/1633 [==============================] - 575s 352ms/step - loss: 0.0141 - sparse_categorical_crossentropy: 0.0069 - sparse_categorical_accuracy: 0.9988 - scaled_adversarial_loss: 0.0072 - val_loss: 0.0113 - val_sparse_categorical_crossentropy: 0.0053 - val_sparse_categorical_accuracy: 0.9990 - val_scaled_adversarial_loss: 0.0060\n",
      "Epoch 9/10\n",
      "1633/1633 [==============================] - 566s 347ms/step - loss: 0.0135 - sparse_categorical_crossentropy: 0.0068 - sparse_categorical_accuracy: 0.9988 - scaled_adversarial_loss: 0.0067 - val_loss: 0.0121 - val_sparse_categorical_crossentropy: 0.0050 - val_sparse_categorical_accuracy: 0.9991 - val_scaled_adversarial_loss: 0.0071\n",
      "Epoch 10/10\n",
      "1633/1633 [==============================] - 573s 351ms/step - loss: 0.0123 - sparse_categorical_crossentropy: 0.0063 - sparse_categorical_accuracy: 0.9989 - scaled_adversarial_loss: 0.0059 - val_loss: 0.0100 - val_sparse_categorical_crossentropy: 0.0052 - val_sparse_categorical_accuracy: 0.9988 - val_scaled_adversarial_loss: 0.0048\n"
     ]
    },
    {
     "data": {
      "text/plain": "<keras.callbacks.History at 0x2be0d6a2ac0>"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adv_model.fit({'feature': X_train, 'label': y_train}, epochs=10, batch_size=512, validation_data={'feature': X_test, 'label': y_test})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "id": "Pmt7mlJoQ-Np",
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "208905/208905 [==============================] - 1495s 7ms/step - loss: 0.0104 - sparse_categorical_crossentropy: 0.0052 - sparse_categorical_accuracy: 0.9988 - scaled_adversarial_loss: 0.0052\n"
     ]
    },
    {
     "data": {
      "text/plain": "[0.010441967286169529,\n 0.005237121134996414,\n 0.9988032579421997,\n 0.005204540211707354]"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "adv_model.evaluate({'feature':X_test, 'label':y_test}, batch_size=1)"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": [],
   "mount_file_id": "1l_2YpamsRqa2kvQbF6VcRh1L-ZVNw2LN",
   "authorship_tag": "ABX9TyOZ6tqQxYPy3g7WnP4d6Wrs"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}